package helper

type SortParamType interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

func QuickSort[T SortParamType](arr []T, left int, right int) {

	if left >= right {
		return
	}
	p := Partition(arr, left, right)
	if left < p {
		QuickSort(arr, left, p-1)
	}
	if p < right {
		QuickSort(arr, p+1, right)
	}
}

func Partition[T SortParamType](arr []T, left int, right int) int {
	if arr == nil || left >= right || left < 0 || right >= len(arr) {
		return -1
	}
	i, j := left, left
	for j < right {
		if arr[j] < arr[right] {
			if i != j {
				arr[i], arr[j] = arr[j], arr[i]
			}
			i++
		}
		j++
	}
	arr[i], arr[right] = arr[right], arr[i]
	return i
}
